package com.project.market.service.impl;

import com.github.pagehelper.PageHelper;
import com.project.market.db.domain.MarketCollect;
import com.project.market.db.domain.MarketCollectExample;
import com.project.market.db.mapper.MarketCollectMapper;
import com.project.market.service.AdminCollertService;
import com.project.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminCollectServiceImpl implements AdminCollertService {
    @Override
    public List<MarketCollect> list(String page, String limit, String sort, String order, String userId, String valueId) {

        SqlSession session = MybatisUtil.getSession();
        MarketCollectMapper collectMapper = session.getMapper(MarketCollectMapper.class);

        MarketCollectExample collectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (userId != null && !("".equals(userId))) {
            try {
                criteria.andUserIdEqualTo(Integer.valueOf(userId));
            } catch (Exception e) {
                return null;
            }
        }

        if (valueId != null && !("".equals(valueId))) {
            try {
                criteria.andValueIdEqualTo(Integer.valueOf(valueId));
            } catch (Exception e) {
                return null;
            }
        }

        collectExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));

        List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);

        session.commit();
        session.close();

        return marketCollects;
    }
}
